import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'cal.dart';

class CalculatorPage2 extends StatefulWidget {
  CalculatorPage2({Key key, this.title}) : super(key: key);

  final String title;
  static const Color PAGE_COLOR = Colors.black;

  // 按钮
  static const Color NUM_BTN_BG = Color(0xff323232);

  //顶部按钮
  static const Color TOP_BTN_BG = Color(0xFFa6a6a6);

  //右侧按钮
  static const Color RIGHT_BTN_BG = Color(0xFFff9500);

  static const NKeys = [
    "C", "D", "?", "/", //
    "9", "8", "7", "*", //,
    "6", "5", "4", "-", //
    "3", "2", "1", "+", //
    "", "0", ".", "=", //
  ];

//顶部按钮
  static const TKeys = [
    "C",
    "D",
    "?",
  ];

//右侧按钮
  static const RKeys = ["/", "*", "-", "+", "="];

  @override
  _CalculatorPage2State createState() => _CalculatorPage2State();
}

class _CalculatorPage2State extends State<CalculatorPage2> {
  String _num = "";
  Cal _cal = new Cal();

  void clickKey(String key) {
    _cal.addKey(key);
    setState(() {
      _num = _cal.OutPut;
    });
  }

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      backgroundColor: CalculatorPage2.PAGE_COLOR,
      // appBar: AppBar(
      //   title: Text("计算器"),
      //   backgroundColor: CalculatorPage2.PAGE_COLOR,
      //   centerTitle: true,
      // ),
      child: Padding(
        padding: const EdgeInsets.all(10.0),
        child: Container(
          child: Column(
            children: <Widget>[
              Expanded(
                child: SingleChildScrollView(
                  reverse: true,
                  child: Padding(
                    padding: const EdgeInsets.symmetric(
                        horizontal: 20.0, vertical: 10.0),
                    child: Align(
                      alignment: Alignment.bottomRight,
                      child: Text(
                        "$_num",
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 32.0,
                        ),
                      ),
                    ),
                  ),
                ),
              ),
              Container(child: Center(child: _buildBtns()))
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildBtns() {
    List<Widget> rows = [];

    List<Widget> btns = [];
    int flex = 1;
    for (int i = 0; i < CalculatorPage2.NKeys.length; i++) {
      String key = CalculatorPage2.NKeys[i];
      if (key.isEmpty) {
        flex++;
        continue;
      } else {
        Widget b = buildTextButton(key, flex: flex);
        btns.add(b);
        flex = 1;
      }

      if ((i + 1) % 4 == 0) {
        rows.add(Row(
          children: btns,
        ));
        btns = [];
      }
    }
    if (btns.length > 0) {
      rows.add(Row(
        children: btns,
      ));
      btns = [];
    }

    return Column(
      children: rows,
    );
  }

  Widget buildTextButton(String num, {int flex = 1}) {
    print('num = ' + num + "  flex = " + flex.toString());

    return Expanded(
      flex: flex,
      child: TextButton(
        onPressed: () {
          clickKey(num);
        },
        // padding: EdgeInsets.all(0.0),
        child: Container(
          decoration: BoxDecoration(
              color: CalculatorPage2.TKeys.contains(num)
                  ? CalculatorPage2.TOP_BTN_BG
                  : CalculatorPage2.RKeys.contains(num)
                      ? CalculatorPage2.RIGHT_BTN_BG
                      : CalculatorPage2.NUM_BTN_BG,
              shape: flex > 1 ? BoxShape.rectangle : BoxShape.circle,
              borderRadius:
                  flex > 1 ? BorderRadius.all(Radius.circular(90.0)) : null),
          padding: EdgeInsets.all(30.0),
          // margin: EdgeInsets.all(10.0),
          child: Center(
            child: Text(
              "$num",
              style: TextStyle(fontSize: 28.0, color: Colors.white),
            ),
          ),
        ),
      ),
    );
  }
}
